﻿#include "X2EWidget.h"
#include "ui_X2EWidget.h"
#include "XmlManager.h"
#include "XlsxManager.h"

#include "Commons.h"

#include <QDomElement>
#include <QDebug>
#include <QXmlStreamWriter>
#include <QFile>

X2EWidget::X2EWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::X2EWidget)
{
    ui->setupUi(this);

    // 加载xml文件
    loadXml();

    // 加载xlsx文件
    loadXlsx();
}

X2EWidget::~X2EWidget()
{
    delete ui;
}

void X2EWidget::loadXml()
{
    QString filePath = QCoreApplication::applicationDirPath() + "/test.xml";
    if(XmlManager::instance().loadXmlFile(filePath))
    {
        // load success
        QDomElement root = XmlManager::instance().getRootElement();
        qDebug() << root.tagName();
        QString tagName = "Group";
        QDomNodeList groups = XmlManager::instance().getElementsByTagName(tagName);
        for(int i = 0 ; i < groups.count(); ++i){
            QDomElement groupElement = groups.item(i).toElement();
            QString name_group = groupElement.attribute(XML_ATTRIBUTE_NAME);
            QString uuid_group = groupElement.attribute(XML_ATTRIBUTE_UUID);
            qDebug()  << name_group << uuid_group;

            QDomNodeList groupChildren = groupElement.childNodes();
            for(int j = 0 ; j < groupChildren.count(); ++j){
                QDomElement icdDocElement = groupChildren.item(j).toElement();
                QString name_icdDoc = icdDocElement.attribute(XML_ATTRIBUTE_NAME);
                QString uuid_icdDoc = icdDocElement.attribute(XML_ATTRIBUTE_UUID);
                QString messageCode_icdDoc = icdDocElement.attribute(XML_ATTRIBUTE_MESSAGE_CODE);
                qDebug()  << name_icdDoc << uuid_icdDoc << messageCode_icdDoc;

                QDomNodeList icdDocChildren = icdDocElement.childNodes();
                for(int k = 0 ; k < icdDocChildren.count(); ++k){
                    QDomElement fieldElement = icdDocChildren.item(j).toElement();
                    QString name_field = fieldElement.attribute(XML_ATTRIBUTE_NAME);
                    QString uuid_field = fieldElement.attribute(XML_ATTRIBUTE_UUID);
                    QString type_field = fieldElement.attribute(XML_ATTRIBUTE_TYPE);
                    QString size_field = fieldElement.attribute(XML_ATTRIBUTE_SIZE);
                    qDebug()  << name_field << uuid_field << type_field << size_field;
                }
            }
        }
        //        QList<QDomElement> documents = XmlManager::instance().getChildElements("Document");
        //        for(const QDomElement& doc : documents){
        //            QString tagName = doc.tagName();
        //            QList<QDomElement> groups = XmlManager::instance().getChildElements(tagName);
        //            for(const QDomElement& group : groups)
        //            {
        //                QString tagName = group.tagName();
        //                QList<QDomElement> icdDocs = XmlManager::instance().getChildElements(tagName);

        //                for(const QDomElement& icdDoc : icdDocs)
        //                {
        //                    QString tagName = icdDoc.tagName();
        //                    QList<QDomElement> fields = XmlManager::instance().getChildElements(tagName);

        //                    for(const QDomElement& field : fields)
        //                    {
        //                        QString tagName = field.tagName();
        //                        qDebug() << "field" << tagName;
        //                    }
        //                }
        //            }
        //        }
    }
    else{
        qDebug() << "load xml failed";
    }
}

void X2EWidget::loadXlsx()
{
    QString xlsxPath = QCoreApplication::applicationDirPath() + "/tes.xlsx";
    XlsxManager::instance().loadXlsxFile(xlsxPath);
}

bool X2EWidget::excelToXml(const QString &xlsxPath, const QString &xmlPath)
{
    Q_UNUSED(xlsxPath);
    Q_UNUSED(xmlPath);
    // QXlsx::Document xlsx(xlsxPath);
    return true;
}

